'use strict'
//数据存储在localStorage  保存于本地浏览器cookie;
yjx.factory('$locals', ["$q", "$http", '$window', function ($q, $http, $window) {
    var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    return {
        //存储单个属性
        set: function (key, value) {
            $window.localStorage[key] = value;
        },
        //读取单个属性
        get: function (key, defaultValue) {
            return $window.localStorage[key] || defaultValue;
        },
        //清空单个属性
        remove: function (key) {
            return $window.localStorage[key] = '';
        },
        //存储对象，以JSON格式存储
        setObject: function (key, value) {
            $window.localStorage[key] = JSON.stringify(value);
        },
        //读取对象
        getObject: function (key) {
            return JSON.parse($window.localStorage[key] || '{}');
        },
        /*$locals 数组操作 输出数组：arr(key)；添加：push(key,value)；删除：cut(key,value)；*/
        //输出数组：
        arr: function (key) {
            if (typeof($window.localStorage[key]) == 'undefined') {
                //console.info(undefined+'获取数组异常');
            } else {
                return $window.localStorage[key].split(',');
            }
        },
        //存储数组到key
        push: function (key, value) {
            var arr = new Array();
            if (typeof($window.localStorage[key]) == 'undefined' || $window.localStorage[key] == '') {
                arr.push([value]);
            } else {
                var arrs = $window.localStorage[key];
                arr = arrs.split(',');
                arr.push(value);
                //console.info(arr);
            }
            $window.localStorage[key] = arr;
        },
        //删除key数组的值
        cut: function (key, value) {
            if (typeof($window.localStorage[key]) == 'undefined') {
                //console.info(undefined+'获取数组异常');
            } else {
                var arr = new Array();
                var arrs = $window.localStorage[key];
                arr = arrs.split(',');
                var count = 0;
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i] == value) {
                        count++;
                        arr.splice(i, 1);//arr.splice(deletePos,deleteCount);删除从指定位置deletePos开始的指定数量deleteCount的元素，数组形式返回所移除的元素
                    }
                }
                if (count == 0) {
                    //console.log(undefined + '数组值不存在');
                }
            }
            $window.localStorage[key] = arr;
        },
        randomStr: function (n) {
            var res = "";
            for (var i = 0; i < n; i++) {
                var id = Math.ceil(Math.random() * 35);
                res += chars[id];
            }
            return res;
        }
    }
}]).factory('$init', ['$rootScope', 'ipCookie', '$timeout', '$location', '$state',
    function ($rootScope, ipCookie, $timeout, $location, $state) {
        return {
            cleanCookies: function () {
                $timeout(function () {
                    //清除$roosScope cookie 缓存
                    $rootScope.nickname = null;
                    $rootScope.telephone = null;
                    $rootScope.avatar = null;
                    $rootScope.Uid = null;
                    $rootScope.user = null;
                    ipCookie.remove('Uid', ipCookiesParam);
                    ipCookie.remove('avatar', ipCookiesParam);
                    ipCookie.remove('user', ipCookiesParam);
                    ipCookie.remove('nickname', ipCookiesParam);
                    //$rootScope.modalStatus = 'modal-hide';
                })
            },
            initQQlogin: function () {
                angular.element(document.querySelector('#qqLoginBtn')).find('a').html('<img class="ektimg" src="common/img/QQ_03.jpg" />');
            },
            loadingOn:function(){//全屏加载等待提示；
                $rootScope.modalLoading.class = 'loading';
                $rootScope.modalLoading.msg = '登录中...';
            },
            loadingOff:function(){//全屏加载等待提示；
                $rootScope.modalLoading.class = '';
            },
            title:function(title){
                title = title?title:'基因云';
                angular.element(document.querySelector('#title')).html(title+' - 基因云')
            }
        }
    }]);